import { Model as BaseModel } from './models'
import controllers from './controllers'

const web = controllers.web

export class Environment {
  constructor(payload = {}) {
    const { context } = payload

    if (context) this._context = context
    else {
      const ctx = web.session.user_context
      this._context = ctx
    }
  }

  get context() {
    return this._context
  }

  model(model) {
    return this._create_model_class({ model })
  }

  _create_model_class({ model }) {
    class Model extends BaseModel {
      constructor(...args) {
        super(...args)
      }
    }

    const cls_name = model.replace('.', '_')
    Object.defineProperty(Model, 'name', { value: cls_name })
    Model._env = this
    Model._model = model
    return Model
  }
}
